def forwardbackward(t0, func, alpha, p):
    t1 = t0 + alpha * p
    n = 1
    if func(t1) > func(t0):
        return [backward(t0, t1, func, alpha, n, p), t1]
    else:
        return [t0, forward(t0, t1, func, alpha, n, p)]


def backward(x0, x1, func, alpha, n, p):
    if n == 1:
        n = 0
    else:
        alpha = 2 * alpha
    x2 = x0 - alpha * p
    if func(x2) < func(x0):
        return backward(x0, x2, func, alpha, n, p)
    else:
        return x2


def forward(x0, x1, func, alpha, n, p):
    if n == 0:
        n = 1
    else:
        alpha = 2 * alpha
    x2 = x1 + alpha * p
    if func(x2) < func(x1):
        return forward(x1, x2, func, alpha, n, p)
    else:
        return x0
